/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

//TODO: Auto-generated Javadoc
/**
 * The Class LabelFigure.
 * 
 * @author Chokri Mraidha
 */
public class LabelFigure extends Label implements IAbstractElementFigure {

	/**
	 * The stereotypes label.
	 */
	private Label stereotypesLabel;

	/**
	 * The icon label.
	 */
	private Label iconLabel;	

	/**
	 * The background color.
	 */
	protected Color backgroundColor = ColorConstants.lightBlue;

	/**
	 * The border color.
	 */
	protected Color borderColor     = ColorConstants.black;

	/**
	 * The fore ground color.
	 */
	protected Color foreGroundColor = ColorConstants.black;

	/**
	 * The font color.
	 */
	protected Color fontColor       = ColorConstants.black;

	/**
	 * The Line thickness.
	 */
	protected int LineThickness = 1;

	/**
	 * The font size.
	 */
	protected int fontSize      = 10;

	/**
	 * The line stle.
	 */
	protected String lineStle;

	/**
	 * The font string.
	 */
	protected String fontString = "Arial";

	/**
	 * The font.
	 */
	protected Font font = null;

	/**
	 * The img.
	 */
	protected Image img = null;



	/**
	 * Refresh presentation.
	 * 
	 * @param presentation the presentation
	 * @param selection the selection
	 */
	public void refreshPresentation(String presentation, Element selection, GraphElement ge) {
		if(selection!=null){
			org.eclipse.swt.graphics.Image icon =PapyrusPlugin.getIconElement(selection, ge);

			if(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION.equals(presentation)) {
				this.setIcon(null);
			} else if ((icon != null) 
					&& ( (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION))
							|| presentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION))) {
				this.setIcon(icon);
			}
			else {
				this.setIcon(null);
			}
		}
		return;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshStereotypes(java.lang.String, java.lang.String, boolean, boolean)
	 */
	public void refreshStereotypes(String stereotypes, String presentation, boolean hasIcon, boolean hasShape) {

		if (stereotypes.equals("")) {
			this.setStereotypes(null);
			return;
		}

		if (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION) && hasIcon) {
			this.setStereotypes(null);
			return;
		} 

		this.setStereotypes(stereotypes);//add stereotypes on the class figure		
		return;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setStereotypes(java.lang.String)
	 */
	public void setStereotypes(String stereotypes) {		
		if( stereotypes == null ) { 
			// Remove label if any
			if( this.stereotypesLabel != null) {
				this.remove(this.stereotypesLabel);
				this.stereotypesLabel = null;
			}
			return;
		}

		// Set the stereotype label
		if( this.stereotypesLabel == null ) {
			this.createStereotypeLabel();
		}

		// Set stereotype text on figure
		if (!stereotypes.equals("")) {
			this.stereotypesLabel.setText(PapyrusPlugin.ST_LEFT+stereotypes+PapyrusPlugin.ST_RIGHT);	
		} else {
			this.stereotypesLabel.setText("");
		}
	}

	/**
	 * this method is used to create the stereotype label.
	 */
	protected void createStereotypeLabel()
	{
		FontData[] fontdata={new FontData(this.fontString, 8, SWT.NORMAL)};
		Font font = PapyrusPlugin.fontManager.get(fontdata);
		Label label = new Label();
		label.setFont(font);
		label.setOpaque(false);
		// Add the label to the figure, at pos 0
		int index = 0;
		if( this.iconLabel != null) {
			index++;	
		}
		this.add(label, 0);
		this.stereotypesLabel = label;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#getBackgroundColor()
	 */
	@Override
	public Color getBackgroundColor(){ return null;
	//the label is translucide
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#setBackgroundColor(org.eclipse.swt.graphics.Color)
	 */
	@Override
	public void setBackgroundColor(Color backgroundColor){}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getBorderColor()
	 */
	public Color getBorderColor(){
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setBorderColor(org.eclipse.swt.graphics.Color)
	 */
	public void setBorderColor(Color borderColor){}

//	/*
//	* (non-Javadoc)
//	* @see org.eclipse.draw2d.Label#setIcon(org.eclipse.swt.graphics.Image)
//	*/
//	/**
//	* Sets the icon.
//	* 
//	* @param img the img
//	*/
//	@Override
//	public void setIcon(Image img){} 

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getfigureFont()
	 */
	public String getfigureFont(){
		return this.fontString;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setfigureFont(java.lang.String)
	 */
	public void setfigureFont(String font){
		this.fontString=font;	
		FontData[] fontdata={new FontData(font, this.fontSize, SWT.NORMAL)};
		this.font = PapyrusPlugin.fontManager.get(fontdata);
		super.setFont(this.font);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getFontSize()
	 */
	public int getFontSize(){
		return this.fontSize;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setFontSize(int)
	 */
	public void setFontSize(int fontSize){
		FontData[] fontdata={new FontData(this.fontString, fontSize, SWT.NORMAL)};
		this.font = PapyrusPlugin.fontManager.get(fontdata);
		super.setFont(this.font);
		this.fontSize = fontSize;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getForeGroundColor()
	 */
	public Color getForeGroundColor() {
		return this.foreGroundColor;
	}


	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setForeGroundColor(org.eclipse.swt.graphics.Color)
	 */
	public void setForeGroundColor(Color foreGroundColor) {
		this.foreGroundColor = foreGroundColor;
		super.setForegroundColor(foreGroundColor);


	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getLineStle()
	 */
	public String getLineStle() {
		return this.lineStle;
	}


	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setLineStle(java.lang.String)
	 */
	public void setLineStle(String lineStle) {

		this.lineStle = lineStle;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getLineThickness()
	 */
	public int getLineThickness() {
		return this.LineThickness;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setLineThickness(int)
	 */
	public void setLineThickness(int lineThickness) {
		this.LineThickness = lineThickness;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getFontColor()
	 */
	public Color getFontColor() {
		return this.fontColor;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setFontColor(org.eclipse.swt.graphics.Color)
	 */
	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshColors(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	public void refreshColors(GraphElement graphElement) {

		//set colors
		//backgroundColor

		this.setBackgroundColor(PapyrusPlugin.colorManager.get(graphElement.getBackgroundColor()));
		this.setFontColor(PapyrusPlugin.colorManager.get( graphElement.getFontColor()));
		this.setBorderColor(PapyrusPlugin.colorManager.get(graphElement.getBorderColor()));
		this.setForeGroundColor(PapyrusPlugin.colorManager.get(graphElement.getFontColor()));
		this.setFont(PapyrusPlugin.fontManager.get(new FontData[]{new FontData(graphElement.getFontFamily(),graphElement.getFontSize(),SWT.NORMAL)}));
	}




	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshQualifiedName(com.cea.papyrus.diagraminterchange2.di2.GraphElement, com.cea.papyrus.diagraminterchange2.di2.GraphElement, org.eclipse.uml2.uml.NamedElement)
	 */
	public void refreshQualifiedName(GraphElement parentge, GraphElement currentge, NamedElement element){}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setName(java.lang.String)
	 */
	public void setName( String name ){}


	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getNameLabel()
	 */
	public Label getNameLabel(){
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getStereotypesLabel()
	 */
	public Label getStereotypesLabel(){
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setQualifiedName(java.lang.String)
	 */
	public void setQualifiedName( String qualifiedName ){}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getQualifiedNameLabel()
	 */
	public Label getQualifiedNameLabel(){
		return null;
	}

	public Dimension getMinimumDimension(){
		int width=getTextSize().width+30;
		int height= getTextSize().height+1;
		return new Dimension(width, height);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getLabelAt(org.eclipse.draw2d.geometry.Point)
	 */
	public Label getLabelAt(Point pt){
		return null;
	}


}
